package common.itemhandling.files;

import java.io.File;
import java.io.Serializable;
import java.text.Collator;
import java.util.Calendar;
/**
 * @author Julius Ollesch
 * 
 */
public class Version implements Comparable<Version>, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9213393979690152995L;

	private int firstLevel;

	private int secondLevel;

	private int thirdLevel;

	private int revisionNumber;

	public Version() {
		super();
		setFirstLevel(Calendar.getInstance().get(Calendar.YEAR));
		setSecondLevel(Calendar.getInstance().get(Calendar.MONTH));
		setThirdLevel(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
		setRevisionNumber(1);
	}

	public Version(final int first, final int second, final int third) {
		super();
		setFirstLevel(first);
		setSecondLevel(second);
		setThirdLevel(third);
		setRevisionNumber(1);
	}

	public int compareTo(Version v) {
		return Collator.getInstance().compare(this.toString(), v.toString());
	}

	public int getFirstLevel() {
		return firstLevel;
	}

	public int getRevisionNumber() {
		return revisionNumber;
	}

	public int getSecondLevel() {
		return secondLevel;
	}

	public int getThirdLevel() {
		return thirdLevel;
	}

	public void majorUpdate() {
		setSecondLevel(getSecondLevel() + 1);
	}

	public void minorUpdate() {
		setThirdLevel(getThirdLevel() + 1);
	}

	public void newRevision() {
		setRevisionNumber(getRevisionNumber() + 1);
	}

	private void setFirstLevel(int firstLevel) {
		this.firstLevel = firstLevel;
	}

	public void setRevisionNumber(int revisionNumber) {
		this.revisionNumber = revisionNumber;
	}

	private void setSecondLevel(int secondLevel) {
		this.secondLevel = secondLevel;
	}

	private void setThirdLevel(int thirdLevel) {
		this.thirdLevel = thirdLevel;
	}

	@Override
	public String toString() {
		return firstLevel + File.separator + secondLevel + File.separator
				+ thirdLevel + File.separator + revisionNumber;
	}

	public void upgrade() {
		setFirstLevel(getFirstLevel() + 1);
	}

}
